/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CSISL4pk;

/**
 *
 * @author Josh
 */
import javax.swing.UIManager.*;
import javax.swing.*;
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class CSI_GUI extends JFrame
        implements ActionListener
{

    JDesktopPane desktop;
    Boolean invLoggedIn = false;//Investigator loggied in = fullacces else noaccess
    String uNameTextField;
    JLabel welcomeName;
    String pWrdTextField;
    LoginGUI displayFrame;
    GenerateReportGUI reportFrame;
    LoadCaseFrame loadCaseFrame;
    HomePageFrame homePanel;
    EditCaseFrame editFrame;
    SearchFrame sFrame;
    EditCaseFrame nCFrame;
    ReqNewCaseNumSQL c;
    LoginSubmitSQL OK;
    Boolean homePanelOpen = false;
    Case case1 = new Case();
    Session investigator;

    public CSI_GUI()
    {

        super("CSI Salt Lake 4.0");
        try
        {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
            {
                if ("Nimbus".equals(info.getName()))
                {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e)
        {
        }
        setUndecorated(true);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(0, 0, screenSize.width, screenSize.height);

        int inset = 100;

        desktop = new JDesktopPane(); //a specialized layered pane
        createLoginFrame(); //create first "window"
        setContentPane(desktop);
        setJMenuBar(createMenuBar());

        //Make dragging a little faster but perhaps uglier.
        desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);

    }

    protected JMenuBar createMenuBar()
    {
        JMenuBar menuBar = new JMenuBar();
        //Set up the menus.
        JMenu start = new JMenu(" -Start- ");
        start.setFont(new Font("SansSerif", 1, 14));
        menuBar.add(start);

        JMenu menu = new JMenu(" -Case-File- ");
        menu.setMnemonic(KeyEvent.VK_D);
        menu.setFont(new Font("SansSerif", 0, 14));
        menuBar.add(menu);

        JMenu reports = new JMenu(" -Reports- ");
        reports.setFont(new Font("SansSerif", 0, 14));
        menuBar.add(reports);

        //Set up the menu items.
        JMenuItem menuItem = new JMenuItem("Create Report");
        menuItem.setMnemonic(KeyEvent.VK_R);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_R, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("report");
        menuItem.addActionListener(this);
        reports.add(menuItem);

        menuItem = new JMenuItem("Home Page");
        menuItem.setMnemonic(KeyEvent.VK_H);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_H, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("home");
        menuItem.addActionListener(this);
        start.add(menuItem);

        menuItem = new JMenuItem("New Case");
        menuItem.setMnemonic(KeyEvent.VK_N);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_N, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("new case");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        menuItem = new JMenuItem("Load Case");
        menuItem.setMnemonic(KeyEvent.VK_L);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_L, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("load");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        menuItem = new JMenuItem("Login");
        menuItem.setMnemonic(KeyEvent.VK_I);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_I, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("login");
        menuItem.addActionListener(this);
        start.add(menuItem);

        menuItem = new JMenuItem("Logout");
        menuItem.setMnemonic(KeyEvent.VK_T);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_T, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("logout");
        menuItem.addActionListener(this);
        start.add(menuItem);

        menuItem = new JMenuItem("Shutdown");
        menuItem.setMnemonic(KeyEvent.VK_Q);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_Q, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("quit");
        menuItem.addActionListener(this);
        start.add(menuItem);

        return menuBar;
    }

    //React to menu selections.
    public void actionPerformed(ActionEvent e)
    {

        if ("load".equals(e.getActionCommand()))
        {//load case window
            if (invLoggedIn == true)
            {
                if (homePanel.isVisible())
                {

                    homePanel.setVisible(false);//set home page invisible
                    loadCaseFrame("");
                } else
                {
                    loadCaseFrame("");
                }
            } else
            {
                JOptionPane.showMessageDialog(this, "You Are NOT Logged In!!", "Login Error", JOptionPane.ERROR_MESSAGE);
            }
        } else if ("login".equals(e.getActionCommand()))
        {//opens login window
            //if already logged in display error
            if (invLoggedIn == true)
            {
                JOptionPane.showMessageDialog(this, "You Are Already Logged In.", "Login Error", JOptionPane.ERROR_MESSAGE);
            } else
            {
                createLoginFrame();
            }
        } else if ("logout".equals(e.getActionCommand()))
        {//resets to logged out mode
            if (invLoggedIn == true)
            {
                LogoutSubmitSQL out;
                out = new LogoutSubmitSQL();
                investigator.setEndTime();
                out.logoutNow(investigator.getUserId(), investigator.getStartTime(), investigator.getEndTime(), investigator.getEdit());
                //close all windows begin createloginGUI
                invLoggedIn = out.isLoggedIn();
                if (invLoggedIn == false)
                {

                    int i = desktop.getAllFrames().length;//count how many open windows
                    while (i > 0)//close all open windows until none open
                    {
                        i--;//decrement i
                        desktop.getSelectedFrame().doDefaultCloseAction();//close open frame

                    }
                    createLoginFrame();//reopens the login page
                    JOptionPane.showMessageDialog(this, "You Are Now Logged Out.", "Logout Confirmed", JOptionPane.WARNING_MESSAGE);
                } else
                {
                    JOptionPane.showMessageDialog(this, "An ERROR Occured!!", "Logout Error", JOptionPane.ERROR_MESSAGE);
                }
            } else
            {
                JOptionPane.showMessageDialog(this, "You Are NOT Logged In!!", "Login Error", JOptionPane.ERROR_MESSAGE);
            }
        } else if ("loginSubmit".equals(e.getActionCommand()))
        {//user sent login info ask SQL if OK

            uNameTextField = displayFrame.uNameTextField.getText();
            pWrdTextField = displayFrame.pWrdTextField.getText();
            //get info from textfields
            String u = uNameTextField;
            String p = pWrdTextField;

            invLoggedIn = sendLogin(u, p);//send it
            if (invLoggedIn == true)
            {
                desktop.getSelectedFrame().doDefaultCloseAction();//close the login frame
                //open home page
                createHomeP();
                homePanel.setTitle("Investigator: " + u);
                homePanelOpen = true;
                JOptionPane.showMessageDialog(this, "You Are Now Logged In.", "Login Confirmed", JOptionPane.WARNING_MESSAGE);
            } else
            {
                JOptionPane.showMessageDialog(this, "You Have Entered Incorrect Information", "Login Denied", JOptionPane.ERROR_MESSAGE);
            }

        } else if ("new case".equals(e.getActionCommand()))
        {//create new case frame
            if (invLoggedIn == true)
            {
                if (homePanel.isVisible())
                {
                    homePanel.setVisible(false);
                    createNewFrame();
                    //populate with caseID(assigned by server)
                    //populate userID or name
                } else
                {
                    createNewFrame();
                }
            } else
            {
                JOptionPane.showMessageDialog(this, "You Are NOT Logged In!!", "Login Error", JOptionPane.ERROR_MESSAGE);
            }
        } else if ("home".equals(e.getActionCommand()))
        {//create home page
            try
            {
                if (homePanelOpen == true && invLoggedIn == true)
                {
                    homePanel.setVisible(true);
                } else if (invLoggedIn == true)//homePanelOpen = false
                {
                    createHomeP();
                } else
                {
                    JOptionPane.showMessageDialog(this, "You Are NOT Logged In!!", "Login Error", JOptionPane.ERROR_MESSAGE);
                }

            } catch (Exception n)
            {
                System.out.println(n);
                if (invLoggedIn == true)
                {
                    createHomeP();
                } else
                {
                    JOptionPane.showMessageDialog(this, "You Are NOT Logged In!!", "Login Error", JOptionPane.ERROR_MESSAGE);
                }
            }

        } else if ("createNewSave".equals(e.getActionCommand()))
        {
            System.out.println("preparing to save...");
            //get info from NewCase Form

            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date a = new Date();
            Address tempA = new Address();
            Address tempVictim = new Address();
            Address tempSuspect = new Address();
            Address tempWitness = new Address();
            case1.setArea(nCFrame.crimeTypeBox.getSelectedItem().toString());
            case1.setLastUpdateTime(dateFormat.format(a));
            case1.setNotes(ParseStringFunctions.parseStringOut(nCFrame.caseDescriptionArea.getText()));
            case1.setType(nCFrame.crimeTypeBox.getSelectedItem().toString());

            case1.setDate(nCFrame.dateYearField.getText() + "-" + nCFrame.dateMonthField.getText() + "-" + nCFrame.dateDayField.getText() + " 08:00:00");
            case1.setArea(nCFrame.locationBox.getSelectedItem().toString());
            tempA.setCity(nCFrame.cityField.getText());
            tempA.setStreet(nCFrame.streetField.getText());
            tempA.setState(nCFrame.stateField.getText());
            System.out.println(tempA.getStreet());
            if (!nCFrame.zipField.getText().isEmpty())
            {
                tempA.setZip(Integer.parseInt(nCFrame.zipField.getText()));
            }
            case1.setLocation(tempA);

            case1.victim.setFirst(nCFrame.victim.firstText.getText());
            case1.victim.setMiddle(nCFrame.victim.middleText.getText());
            case1.victim.setLast(nCFrame.victim.lastText.getText());

            case1.victim.setDob(nCFrame.victim.dobY.getText() + "-" + nCFrame.victim.dobM.getText() + "-" + nCFrame.victim.dobD.getText());
            //This errors when null
            if (!nCFrame.victim.genderText.getText().isEmpty())
            {
                case1.victim.setGender(nCFrame.victim.genderText.getText().charAt(0));
            }
            if (!nCFrame.victim.ssnText.getText().isEmpty())
            {
                case1.victim.setSSN(nCFrame.victim.ssnText.getText());
            }
            tempVictim.setApt(nCFrame.victim.unitText.getText());
            tempVictim.setCity(nCFrame.victim.cityField.getText());
            tempVictim.setState(nCFrame.victim.stateField.getText());
            tempVictim.setStreet(nCFrame.victim.streetField.getText());
            if (!nCFrame.victim.zipField.getText().isEmpty())
            {
                tempVictim.setZip(Integer.parseInt(nCFrame.victim.zipField.getText()));
            }
            case1.victim.setAddress(tempVictim);

            case1.suspect.setFirst(nCFrame.suspect.firstText.getText());
            case1.suspect.setMiddle(nCFrame.suspect.middleText.getText());
            case1.suspect.setLast(nCFrame.suspect.lastText.getText());

            case1.suspect.setDob(nCFrame.suspect.dobY.getText() + "-" + nCFrame.suspect.dobM.getText() + "-" + nCFrame.suspect.dobD.getText());
            if (!nCFrame.suspect.genderText.getText().isEmpty())
            {
                case1.suspect.setGender(nCFrame.suspect.genderText.getText().charAt(0));
            }
            tempSuspect.setApt(nCFrame.suspect.unitText.getText());
            tempSuspect.setCity(nCFrame.suspect.cityField.getText());
            tempSuspect.setState(nCFrame.suspect.stateField.getText());
            tempSuspect.setStreet(nCFrame.suspect.streetField.getText());
            if (!nCFrame.suspect.zipField.getText().isEmpty())
            {
                tempSuspect.setZip(Integer.parseInt(nCFrame.suspect.zipField.getText()));
            }
            case1.suspect.setAddress(tempSuspect);

            if (!nCFrame.suspect.ssnText.getText().isEmpty())
            {
                case1.suspect.setSSN(nCFrame.suspect.ssnText.getText());
            }

            case1.witness.setFirst(nCFrame.witness.firstText.getText());
            case1.witness.setMiddle(nCFrame.witness.middleText.getText());

            case1.witness.setLast(nCFrame.witness.lastText.getText());
            case1.witness.setDob(nCFrame.witness.dobY.getText() + "-" + nCFrame.victim.dobM.getText() + "-" + nCFrame.victim.dobD.getText());
            System.out.println("get dob: gui: " + case1.witness.getDob());
            if (!nCFrame.witness.genderText.getText().isEmpty())
            {
                case1.witness.setGender(nCFrame.witness.genderText.getText().charAt(0));
            }
            tempWitness.setApt(nCFrame.witness.unitText.getText());
            tempWitness.setCity(nCFrame.witness.cityField.getText());
            tempWitness.setState(nCFrame.witness.stateField.getText());
            tempWitness.setStreet(nCFrame.witness.streetField.getText());
            if (!nCFrame.witness.zipField.getText().isEmpty())
            {
                tempWitness.setZip(Integer.parseInt(nCFrame.witness.zipField.getText()));
            }
            case1.witness.setAddress(tempWitness);
            if (!nCFrame.witness.ssnText.getText().isEmpty())
            {
                case1.witness.setSSN(nCFrame.witness.ssnText.getText());
            }
            //submit to case->sql

            case1.submitNewCase();

            desktop.getSelectedFrame().doDefaultCloseAction();
            homePanel.setVisible(true);

            //else if error, shout out!

        } else if ("Cancel".equals(e.getActionCommand()))
        {
            //do nothing and close the window
            desktop.getSelectedFrame().doDefaultCloseAction();
            homePanel.setVisible(true);
        } else if ("report".equals(e.getActionCommand()))
        {
            //Open the Generate Reports Frame
            if (invLoggedIn == true)
            {
                if (homePanel.isVisible())
                {
                    homePanel.setVisible(false);
                    generateReportFrame();
                } else
                //create report dialog
                {
                    generateReportFrame();
                }
            } else
            {
                JOptionPane.showMessageDialog(this, "You Are NOT Logged In!!", "Login Error", JOptionPane.ERROR_MESSAGE);
            }
        } else if ("buildReport".equals(e.getActionCommand()))
        {
            //send query to SQL and get back results
        } else if ("printReport".equals(e.getActionCommand()))
        {
            //get results from text area and save
        } else if ("loadCaseSubmit".equals(e.getActionCommand()))
        {
            //from load case number frame
            if (case1.getCaseNum() == Integer.parseInt(loadCaseFrame.cNumTextField.getText()))
            //if(case1.getCaseNum() == new Integer(loadCaseFrame.cNumTextField.getText()))
            {
                //in this case the case number to load is already loaded
                desktop.getSelectedFrame().doDefaultCloseAction();

            } else
            {//the case number does not equal the input number
                case1.setCaseNum(Integer.parseInt(loadCaseFrame.cNumTextField.getText()));
                desktop.getSelectedFrame().doDefaultCloseAction();
                //submit case query to sql
                //if sql does not return error, load values to global elements
                case1.loadCase(case1.getCaseNum());
                //case1.setNotes(ParseStringFunctions.parseStringIn(case1.getNotes()));// parse notes

            }
            editLoadedCase();
            //Here's where I try to system.out the case.
            //System.out.println(case1);
            String delims = "[- ]";
            //then load global elements to form editFrame.
            //like this:
            editFrame.caseNumberField.setText(String.valueOf(case1.getCaseNum()));
            editFrame.investigatorIdField.setText(String.valueOf(case1.getInvId()));

            //date
            String date = case1.getDate();
            if (date != null)
            {
                String[] tokens = date.split(delims);
                editFrame.dateYearField.setText(tokens[0]);
                editFrame.dateMonthField.setText(tokens[1]);
                editFrame.dateDayField.setText(tokens[2]);
            }

            //New lines of code -John
            editFrame.victim.firstText.setText(case1.victim.getFirst());
            editFrame.victim.lastText.setText(case1.victim.getLast());
            editFrame.victim.middleText.setText(case1.victim.getMiddle());
            editFrame.victim.ssnText.setText(case1.victim.getSSN());
            editFrame.victim.genderText.setText(String.valueOf(case1.victim.getGender()));

            String victimDate = case1.victim.getDob();
            if (victimDate != null)
            {
                String[] victimTokens = victimDate.split(delims);
                System.out.println(victimTokens);
                editFrame.victim.dobY.setText(victimTokens[0]);
                editFrame.victim.dobM.setText(victimTokens[1]);
                editFrame.victim.dobD.setText(victimTokens[2]);
            } else
            {
            }

            editFrame.witness.firstText.setText(case1.witness.getFirst());
            editFrame.witness.lastText.setText(case1.witness.getLast());
            editFrame.witness.middleText.setText(case1.witness.getMiddle());
            editFrame.witness.ssnText.setText(case1.witness.getSSN());
            editFrame.witness.genderText.setText(String.valueOf(case1.witness.getGender()));

            String witnessDate = case1.witness.getDob();
            if (witnessDate != null)
            {
                String[] witnessTokens = witnessDate.split(delims);
                editFrame.witness.dobY.setText(witnessTokens[0]);
                editFrame.witness.dobM.setText(witnessTokens[1]);
                editFrame.witness.dobD.setText(witnessTokens[2]);
            }


            editFrame.suspect.firstText.setText(case1.suspect.getFirst());
            editFrame.suspect.lastText.setText(case1.suspect.getLast());
            editFrame.suspect.middleText.setText(case1.suspect.getMiddle());
            editFrame.suspect.ssnText.setText(case1.suspect.getSSN());
            editFrame.suspect.genderText.setText(String.valueOf(case1.suspect.getGender()));
            String suspectDate = case1.suspect.getDob();
            if (suspectDate != null)
            {
                String[] suspectTokens = suspectDate.split(delims);
                editFrame.suspect.dobY.setText(suspectTokens[0]);
                editFrame.suspect.dobM.setText(suspectTokens[1]);
                editFrame.suspect.dobD.setText(suspectTokens[2]);
            }

            editFrame.caseDescriptionArea.setText(String.valueOf(ParseStringFunctions.parseStringIn(case1.getNotes())));
            editFrame.locationBox.setSelectedItem(String.valueOf(case1.getArea()));
            editFrame.crimeTypeBox.setSelectedItem(String.valueOf(case1.getType()));
            try
            {
                editFrame.streetField.setText(case1.location.getStreet());
            } catch (NullPointerException abc)
            {
                editFrame.streetField.setText("UNKNOWN");
            }
            try
            {
                editFrame.cityField.setText(case1.location.getCity());
            } catch (NullPointerException abc)
            {
                editFrame.cityField.setText("UNKNOWN");
            }
            try
            {
                editFrame.stateField.setText(case1.location.getState());
            } catch (NullPointerException abc)
            {
                editFrame.stateField.setText("UK");
            }
            try
            {
                editFrame.zipField.setText(Integer.toString(case1.location.getZip()));
            } catch (NullPointerException abc)
            {
                editFrame.zipField.setText("0");
            }
            //homePanel.setVisible(true);

            //Here's 15 blocks of try and catch to address the addresses fields.
            try//1 street
            {
                editFrame.victim.streetField.setText(case1.victim.address.getStreet());
            } catch(NullPointerException abc)
            {
                editFrame.victim.streetField.setText("UNKNOWN");
            }
            try//2 city
            {
                editFrame.victim.cityField.setText(case1.victim.address.getCity());
            } catch (NullPointerException abc)
            {
                editFrame.victim.cityField.setText("UNKNOWN");
            }
            try//3 state
            {
                editFrame.victim.stateField.setText(case1.victim.address.getState());
            } catch (NullPointerException abc)
            {
                editFrame.victim.stateField.setText("");
            }
            try// 4 zip
            {
                editFrame.victim.zipField.setText(Integer.toString(case1.victim.address.getZip()));
            } catch (NullPointerException abc)
            {
                editFrame.victim.zipField.setText("0");
            }
            try//5 unit
            {
                editFrame.victim.unitText.setText(case1.victim.address.getApt());
            } catch (NullPointerException abc)
            {
                editFrame.victim.unitText.setText("");
            }
            try//1 street
            {
                editFrame.suspect.streetField.setText(case1.suspect.address.getStreet());
            } catch(NullPointerException abc)
            {
                editFrame.suspect.streetField.setText("UNKNOWN");
            }
            try//2 city
            {
                editFrame.suspect.cityField.setText(case1.suspect.address.getCity());
            } catch (NullPointerException abc)
            {
                editFrame.suspect.cityField.setText("UNKNOWN");
            }
            try//3state
            {
                editFrame.suspect.stateField.setText(case1.suspect.address.getState());
            } catch (NullPointerException abc)
            {
                editFrame.suspect.stateField.setText("");
            }
            try//4 zip
            {
                editFrame.suspect.zipField.setText(Integer.toString(case1.suspect.address.getZip()));
            } catch (NullPointerException abc)
            {
                editFrame.suspect.zipField.setText("0");
            }
            try// 5 unit
            {
                editFrame.suspect.unitText.setText(case1.suspect.address.getApt());
            } catch (NullPointerException abc)
            {
                editFrame.suspect.unitText.setText("");
            }
            try//1 street
            {
                editFrame.witness.streetField.setText(case1.witness.address.getStreet());
            } catch(NullPointerException abc)
            {
                editFrame.witness.streetField.setText("UNKNOWN");
            }
            try//2 city
            {
                editFrame.witness.cityField.setText(case1.witness.address.getCity());
            } catch (NullPointerException abc)
            {
                editFrame.witness.cityField.setText("UNKNOWN");
            }
            try//3 state
            {
                editFrame.witness.stateField.setText(case1.witness.address.getState());
            } catch (NullPointerException abc)
            {
                editFrame.witness.stateField.setText("");
            }
            try// 4 zip
            {
                editFrame.witness.zipField.setText(Integer.toString(case1.witness.address.getZip()));
            } catch (NullPointerException abc)
            {
                editFrame.witness.zipField.setText("0");
            }
            try//5 unit
            {
                editFrame.witness.unitText.setText(case1.witness.address.getApt());
            } catch (NullPointerException abc)
            {
                editFrame.witness.unitText.setText("");
            }
        } else if ("saveIT".equals(e.getActionCommand()))

        {
            // **** ALL CHANGES WILL BE SUBMITTED**********
            //called from editCaseFrame
            //save edited case and close
            //send the UPDATE to SQL
            System.out.println("preparing to save...");
            //get info from NewCase Form

            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date a = new Date();
            Address tempA = new Address();
            Address tempVictim = new Address();
            Address tempSuspect = new Address();
            Address tempWitness = new Address();

            case1.setArea(editFrame.crimeTypeBox.getSelectedItem().toString());

            case1.setLastUpdateTime(dateFormat.format(a));

            case1.setNotes(ParseStringFunctions.parseStringOut(editFrame.caseDescriptionArea.getText()));
            case1.setType(editFrame.crimeTypeBox.getSelectedItem().toString());


            case1.setNotes(ParseStringFunctions.parseStringOut(editFrame.caseDescriptionArea.getText()));
            case1.setType(editFrame.crimeTypeBox.getSelectedItem().toString());


            case1.setDate(editFrame.dateYearField.getText() + "-" + editFrame.dateMonthField.getText() + "-" + editFrame.dateDayField.getText() + " 08:00:00");
            case1.setArea(editFrame.locationBox.getSelectedItem().toString());
            tempA.setCity(editFrame.cityField.getText());
            tempA.setStreet(editFrame.streetField.getText());
            tempA.setState(editFrame.stateField.getText());
            if (!editFrame.zipField.getText().isEmpty())

            {
                tempA.setZip(Integer.parseInt(editFrame.zipField.getText()));
            }
            case1.setLocation(tempA);


            case1.victim.setFirst(editFrame.victim.firstText.getText());
            case1.victim.setMiddle(editFrame.victim.middleText.getText());
            case1.victim.setLast(editFrame.victim.lastText.getText());

            case1.victim.setDob(editFrame.victim.dobY.getText() + "-" + editFrame.victim.dobM.getText() + "-" + editFrame.victim.dobD.getText());
            //This errors when null
            if (!editFrame.victim.genderText.getText().isEmpty())
            {
                case1.victim.setGender(editFrame.victim.genderText.getText().charAt(0));//.charAt(0)
            }
            if (!editFrame.victim.ssnText.getText().isEmpty())
            {
                case1.victim.setSSN(editFrame.victim.ssnText.getText());
            }
            tempVictim.setApt(editFrame.victim.unitText.getText());
            tempVictim.setCity(editFrame.victim.cityField.getText());
            tempVictim.setState(editFrame.victim.stateField.getText());
            tempVictim.setStreet(editFrame.victim.streetField.getText());
            if (!editFrame.victim.zipField.getText().isEmpty())
            {
                tempVictim.setZip(Integer.parseInt(editFrame.victim.zipField.getText()));
            }
            case1.victim.setAddress(tempVictim);

            case1.suspect.setFirst(editFrame.suspect.firstText.getText());
            case1.suspect.setMiddle(editFrame.suspect.middleText.getText());
            case1.suspect.setLast(editFrame.suspect.lastText.getText());

            case1.suspect.setDob(editFrame.suspect.dobY.getText() + "-" + editFrame.suspect.dobM.getText() + "-" + editFrame.suspect.dobD.getText());
            if (!editFrame.suspect.genderText.getText().isEmpty())
            {
                case1.suspect.setGender(editFrame.suspect.genderText.getText().charAt(0));
            }
            tempSuspect.setApt(editFrame.suspect.unitText.getText());
            tempSuspect.setCity(editFrame.suspect.cityField.getText());
            tempSuspect.setState(editFrame.suspect.stateField.getText());
            tempSuspect.setStreet(editFrame.suspect.streetField.getText());
            if (!editFrame.suspect.zipField.getText().isEmpty())
            {
                tempSuspect.setZip(Integer.parseInt(editFrame.suspect.zipField.getText()));
            }
            case1.suspect.setAddress(tempSuspect);

            if (!editFrame.suspect.ssnText.getText().isEmpty())
            {
                case1.suspect.setSSN(editFrame.suspect.ssnText.getText());
            }

            case1.witness.setFirst(editFrame.witness.firstText.getText());
            case1.witness.setMiddle(editFrame.witness.middleText.getText());

            case1.witness.setLast(editFrame.witness.lastText.getText());
            case1.witness.setDob(editFrame.witness.dobY.getText() + "-" + editFrame.victim.dobM.getText() + "-" + editFrame.victim.dobD.getText());
            System.out.println("get dob: gui: " + case1.witness.getDob());
            if (!editFrame.witness.genderText.getText().isEmpty())
            {
                case1.witness.setGender(editFrame.witness.genderText.getText().charAt(0));
            }
            tempWitness.setApt(editFrame.witness.unitText.getText());
            tempWitness.setCity(editFrame.witness.cityField.getText());
            tempWitness.setState(editFrame.witness.stateField.getText());
            tempWitness.setStreet(editFrame.witness.streetField.getText());
            if (!editFrame.witness.zipField.getText().isEmpty())
            {
                tempWitness.setZip(Integer.parseInt(editFrame.witness.zipField.getText()));
            }
            case1.witness.setAddress(tempWitness);
            if (!editFrame.witness.ssnText.getText().isEmpty())
            {
                case1.witness.setSSN(editFrame.witness.ssnText.getText());
            }
            //submit to case->sql

            case1.submitEditCase();


            desktop.getSelectedFrame().doDefaultCloseAction();
            homePanel.setVisible(true);
        } else if ("find case".equals(e.getActionCommand()))
        {
            //creating the searchFrame and set it visible

            homePanel.setVisible(false);
            searchFrame();
        } else if ("CancelNew".equals(e.getActionCommand()))
        {
            c.cancelReq();
            desktop.getSelectedFrame().doDefaultCloseAction();
            homePanel.setVisible(true);
        } else if ("search SQL".equals(e.getActionCommand()))
        {
            // you are searching for cases
            //get the data from the search criteria(check the checkboxes,
            //if it is checked get the info)
            //search SQL by query and return to list of [case#, date, victim name, summary]
            desktop.getSelectedFrame().setLayer(2);// the following is is not neccesary, I just wanted it to have something happen
            homePanel.setVisible(true);
        } else if ("searchLoad SQL".equals(e.getActionCommand()))
        {
            //a item should have been selected
            //get that item and do a sql query for that case number
            //set global vars to the proper place from sql return 
            //desktop.getSelectedFrame().setLayer(0);

            desktop.getDesktopManager().iconifyFrame(sFrame);
            String loadCaseNum;
            loadCaseNum = sFrame.caseList.getSelectedValue().toString();
            homePanel.setVisible(false);
            loadCaseFrame(loadCaseNum);
            //set the frame elements = to the global vars
            //see "loadCaseSubmit" action for how to do it

        } else
        { //quit 

            //here is the problem, if you are in the middle of something and quit, you loose data and some created data is unfinished(new case)
            quit();
        }
    }

    //create the HOME Page and listen to actions
    protected void createHomeP()
    {
        homePanel = new HomePageFrame(investigator.getUserId());
        homePanel.createCaseButton.addActionListener(this);
        homePanel.createCaseButton.setActionCommand("new case");
        homePanel.findCaseButton.addActionListener(this);
        homePanel.findCaseButton.setActionCommand("find case");
        homePanel.loadCaseButton.addActionListener(this);
        homePanel.loadCaseButton.setActionCommand("load");
        homePanel.reportButton.addActionListener(this);
        homePanel.reportButton.setActionCommand("report");
        homePanel.statsButton.addActionListener(this);
        homePanel.statsButton.setActionCommand("logout");
        homePanel.lastLoginDispLabel.setText(OK.lastLoginDatetime);
        homePanel.setLayout(null);
        //homePanel.setBounds(WIDTH, WIDTH, WIDTH, WIDTH);
        homePanel.setVisible(true);
        homePanel.setClosable(false);

        desktop.add(homePanel);


    }

    //create report GUI and listen
    protected void generateReportFrame()
    {
        reportFrame = new GenerateReportGUI();
        reportFrame.setVisible(true);
        reportFrame.cancelButton.addActionListener(this);
        reportFrame.cancelButton.setActionCommand("Cancel");
        reportFrame.searchButton.addActionListener(this);
        reportFrame.searchButton.setActionCommand("buildReport");
        reportFrame.printButton.addActionListener(this);
        reportFrame.printButton.setActionCommand("printReport");
        desktop.add(reportFrame);
    }

    //create a search GUI and listen
    protected void searchFrame()
    {
        sFrame = new SearchFrame();
        sFrame.searchButton.addActionListener(this);
        sFrame.searchButton.setActionCommand("search SQL");
        sFrame.loadButton.addActionListener(this);
        sFrame.loadButton.setActionCommand("searchLoad SQL");
        sFrame.setVisible(true);
        desktop.add(sFrame);
    }
    //Use to query SQL to see if user passed in correct params.

    protected Boolean sendLogin(String u, String p)
    {
        //create a loginSQL link
        OK = new LoginSubmitSQL();
        //execute query
        OK.submitIT(u, p);

        if (OK.loginOK.equals(true))
        {
            investigator = new Session(OK.uID, OK.LoginPerms());
            investigator.setEdit(OK.LoginPerms());
            investigator.setUserId(OK.uID);
            case1.setInvId(OK.uID);
            return true;
        } else
        {
            return false;
        }
    }

    //create the "load Case" GUI and watch for actions
    protected void loadCaseFrame(String cNum)
    {
        //all the constructors need to be global, to acces inner parts
        loadCaseFrame = new LoadCaseFrame();
        loadCaseFrame.setVisible(true);
        loadCaseFrame.loadButton.setActionCommand("loadCaseSubmit");
        loadCaseFrame.loadButton.addActionListener(this);
        loadCaseFrame.cancelButton.setActionCommand("Cancel");
        loadCaseFrame.cancelButton.addActionListener(this);
        if (!cNum.isEmpty())
        {
            loadCaseFrame.cNumTextField.setText(cNum);
        }
        desktop.add(loadCaseFrame);
        //SQLLoadCase a = new SQLLoadCase();

        try
        {
            loadCaseFrame.setSelected(true);
        } catch (java.beans.PropertyVetoException e)
        {
        }
    }

    //create the "EDIT CASE" GUI
    protected void editLoadedCase()
    {

        editFrame = new EditCaseFrame(1); // an argument of '1' sets the title of the frame to 'Edit Case'
        editFrame.setVisible(true);
        editFrame.submitButton.setActionCommand("saveIT");
        editFrame.submitButton.addActionListener(this);
        editFrame.cancelButton.setActionCommand("Cancel");
        editFrame.cancelButton.addActionListener(this);
        desktop.add(editFrame);
    }

    //Create a new internal frame.
    protected void createLoginFrame()
    {
        //create the Login GUI
        displayFrame = new LoginGUI();
        //add action control to the main gui
        displayFrame.enterButton.addActionListener(this);
        displayFrame.enterButton.setActionCommand("loginSubmit");
        //use LoginGUI Layout and add to desktop
        displayFrame.setLayout(null);//must set to null to let go of java's preconfig settings
        displayFrame.setVisible(true); //necessary as of 1.3
        //displayFrame.setSize(600,300);
        desktop.add(displayFrame);
    }

    //create a "new case" frame and listent to the actions inside
    protected void createNewFrame()
    {
        nCFrame = new EditCaseFrame(0); // an argument of '0' sets the title of the frame to 'Add Case'

        //request a new case number populate the text field
        //lets look into only doing this when the case is submitted
        requestNewCaseNum("new");

        nCFrame.caseNumberField.setText("" + case1.getCaseNum());
        nCFrame.investigatorIdField.setText("" + investigator.getUserId());//used to insert the current user id
        nCFrame.setVisible(true);
        desktop.add(nCFrame);

        //add action listeners for main gui to react to actions
        nCFrame.submitButton.addActionListener(this);
        nCFrame.submitButton.setActionCommand("createNewSave");
        nCFrame.cancelButton.setActionCommand("CancelNew");
        nCFrame.cancelButton.addActionListener(this);

        try
        {
            nCFrame.setSelected(true);
        } catch (java.beans.PropertyVetoException e)
        {
        }
    }

    //requests a new case number from the system sets the global case number loaded
    protected void requestNewCaseNum(String action)
    {
        c = new ReqNewCaseNumSQL();
        if (action == "new")
        {
            int num = c.begRequest();
            if (num != 0)
            {
                case1.setCaseNum(num);
            } else
            {
                JOptionPane.showMessageDialog(this, "'Request New Case Number' ERROR!!\nPlease See System Administrator.", "Create Case Error", JOptionPane.ERROR_MESSAGE);
            }
        } else if ("cancel".equals(action))
        {
            c.cancelReq();
        } else
        {
            JOptionPane.showMessageDialog(this, "'Request New Case Number' Unknown ERROR!!\nPlease See System Administrator.", "Create Case Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    protected void submitNewCase(int caseNum, String caseDesc, String crimeType, String day,
            String month, String year, String locale, int userID, String street, String city,
            String state, String zip, String suspName, String vicName, String witName)
    {
    }

    //Quit the application.
    protected void quit()
    {
        //In order to prevent someone from logging on and off without it going in the log
        if (invLoggedIn == true)
        {
            LogoutSubmitSQL out;
            out = new LogoutSubmitSQL();
            investigator.setEndTime();
            out.logoutNow(investigator.getUserId(), investigator.getStartTime(), investigator.getEndTime(), investigator.getEdit());
            System.out.println((investigator.getUserId() + "," + investigator.getStartTime() + "," + investigator.getEndTime() + "," + investigator.getEdit()));
        }
        System.exit(0);
    }
}
